// 扩展对象部分的属性
export function partExtend(extendObj: any, sourceObj: any, propList: string[]) {
    for (let i = 0; i < propList.length; i++) {
        const currKey = propList[i];
        if (sourceObj[currKey] !== null) {
            extendObj[currKey] = sourceObj[currKey];
        }
    }
}
// 取得对象属性方法
export function getPropertyValue<T, K extends keyof T>(obj: T, key: K): T[K] {
    return obj[key]
}
// 检查元素是否为DOM
export function checkDOMElem(param: unknown): param is HTMLElement {
    return param instanceof HTMLElement;
}

// 按帧节流方法
export function throttleByFrame(callbackFunc: Function) {
    let canRun = true;
    return function (...params: unknown[]) {
        if (canRun === true) {
            canRun = false;
            const res = callbackFunc(...params);
            requestAnimationFrame(() => {
                canRun = true;
            });
            return res;
        }
    };
}

// 设置translate属性x\y方法
export function setTranslatePosition(x: number, y: number): string {
    // 启用3d加速
    return `
    translate3d(${x}px,${y}px,0)
    `;
}

// 日期格式化方法 formatStr:YYYY-MM-DD hh:mm
export function formatDate(date: Date, formatStr: string): string {
    const fillSingleNum = (numStr: string) => {
        return numStr.length === 1 ? "0" + numStr : numStr;
    };
    // 正则集合类型
    type Rgps = {
        "Y+": string;
        "M+": string;
        "D+": string;
        "h+": string;
        "m+": string;
    }
    // 正则集合
    const rgps: Rgps = {
        "Y+": String(date.getFullYear()),
        "M+": fillSingleNum(String(date.getMonth() + 1)),
        "D+": fillSingleNum(String(date.getDay())),
        "h+": fillSingleNum(String(date.getHours())),
        "m+": fillSingleNum(String(date.getSeconds())),
    };
    // 遍历正则集合替换原字符串
    Object.keys(rgps).forEach((regStr: string) => {
        const currRgp = new RegExp(regStr);
        if (currRgp.test(formatStr)) {
            formatStr = formatStr.replace(currRgp, rgps[regStr as keyof Rgps]);
        }
    });
    return formatStr;
}
